package com.itheima.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.take_delivery.WayBill;
import com.itheima.bos.service.take_delivery.WayBillService;
import com.itheima.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class WayBillAction extends BaseAction<WayBill> {

	private static final Logger LOGGER = Logger.getLogger(WayBillAction.class);

	@Autowired
	private WayBillService wayBillServiceImpl;

	/*
	 * 保存快速录入运单操作
	 */
	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String save() {

		System.out.println(model);
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 去除没有id的order对象
			if (model.getOrder() != null
					&& (model.getOrder().getId() == null || model.getOrder()
							.getId() == 0)) {
				model.setOrder(null);
			}

			wayBillServiceImpl.save(model);
			result.put("success", true);
			result.put("msg", "保存运单成功!");
			LOGGER.info("保存运单成功，运单号：" + model.getWayBillNum());

		} catch (Exception e) {

			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "保存运单失败!");
			LOGGER.info("保存运单失败，运单号：" + model.getWayBillNum());

		}

		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	/*
	 * 分页查询快速录入运单
	 */
	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		Order order = new Order(Direction.DESC, "id");
		Sort sort = new Sort(order);
		Pageable pageable = new PageRequest(page - 1, rows, sort);
		Page<WayBill> pageData = wayBillServiceImpl.findPageData(model,pageable);

		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	/*
	 * 根据运单的运单号查询运单
	 */
	@Action(value = "wayBill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum() {

		WayBill wayBill = wayBillServiceImpl.findByWayBillNum(model.getWayBillNum());
		Map<String, Object> result = new HashMap<>();

		if (wayBill != null) {
			result.put("success", true);
			result.put("wayBillData", wayBill);
		} else {
			result.put("success", false);
		}

		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
